<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class ReservationOperateLogs extends Model
{
    use SoftDeletes;
    protected $table = 'reservation_operate_logs';

    /**
     * 定义主键
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * 禁止批量赋值的字段
     *
     * @var array
     */
    protected $guarded = ['id','created_at','deleted_at'];

    /**
     * 软删除应该被调整为日期的属性
     * [$dates description]
     * @var [type]
     */
    protected $dates=[
        'created_at', 'updated_at','deleted_at'
    ];



    /*
     * 预约操作日志
     * @param int $reservation_id
     * @return bool
     */
    public static function operateLog($reservation_id,$operate_type,$obj_id = 0){

        $user_id = auth() -> id();

        $param = [
            'reservation_id' => $reservation_id,
            'operate_type'   => $operate_type,
            'obj_id'  => $obj_id,
            'user_id' => $user_id,
        ];
        // 对预约操纵
        switch($operate_type){

            case 1: //点赞

                if($obj_id){ //对评论进行点赞或取消点赞
                    if($operateLog = self::where($param) -> first()){
                        $res = $operateLog -> delete();
                        ReservationComment::where('id',$obj_id) -> decrement('zan_num');
                    }else{
                        $res = ReservationOperateLogs::create($param);
                        ReservationComment::where('id',$obj_id) -> increment('zan_num');
                    }
                }else{  // 对预约文章进行点赞
                    if($operateLog = self::where('user_id',$user_id) -> where('reservation_id',$reservation_id) -> where('operate_type',$operate_type) -> first()){
                        $res = $operateLog -> delete();
                        Reservation::where('id',$reservation_id) -> decrement('zan_num');
                    }else{
                        $res = ReservationOperateLogs::create($param);
                        Reservation::where('id',$reservation_id) -> increment('zan_num');
                    }
                }


                break;
            case 2: // 分享或转发

                $res = ReservationOperateLogs::create($param);
                Reservation::where('id',$reservation_id) -> increment('share_num');
                break;
            case 3: // 关注
                if($reservationOperateLogs = ReservationOperateLogs::where($param) -> first()){
                    $res = $reservationOperateLogs -> delete();
                }else{
                    $res = ReservationOperateLogs::create($param);
                }

                if($obj_id){
                    $follow_user_id = ReservationComment::where('id',$obj_id) -> value('user_id');
                }else{
                    $follow_user_id = Reservation::where('id',$reservation_id) -> value('user_id');
                }

                $res2 = UserFollow::followOrCancel($user_id,$follow_user_id,0);
                break;
            case 4: // 评论
                $res = ReservationOperateLogs::create($param);
                //评论数自增
                Reservation::where('id',$reservation_id) -> increment('comment_num');

                break;
        }

        return $res;

    }
}
